package com.cheer.ba.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.presenter.impl.FindOilPresenter;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.TextViewUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.dialog.DialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 用户认证界面
 */
public class UserAuthAty extends BaseCommAty {

    @BindView(R.id.id_user_name_txt)
    TextView mTxtUserName;
    @BindView(R.id.id_user_tel_txt)
    TextView mTxtUserTel;
    @BindView(R.id.id_qye_name_txt)
    TextView mTxtQyeName;
    @BindView(R.id.id_qye_tel_txt)
    TextView mTxtQyeTel;
    @BindView(R.id.id_word_txt)
    TextView mTxtWord;
    @BindView(R.id.id_qye_remark_edit)
    EditText mEditRemark;
    private String mRemark;
    private String mLength;

    private String orgId;
    private String qyeName;
    private String qyeTel;

    public static void startUserAuthAty(Activity activity, String orgId, String qyeName, String qyeTel) {
        Intent intent = new Intent(activity, UserAuthAty.class);
        intent.putExtra(IntentKey.General.KEY_ID, orgId);
        intent.putExtra(IntentKey.General.KEY_DATA, qyeName);
        intent.putExtra(IntentKey.General.KEY_DES, qyeTel);
        activity.startActivity(intent);
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_user_auth, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new FindOilPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText("去认证");

        orgId = getIntent().getStringExtra(IntentKey.General.KEY_ID);
        qyeName = getIntent().getStringExtra(IntentKey.General.KEY_DATA);
        qyeTel = getIntent().getStringExtra(IntentKey.General.KEY_DES);

        mTxtUserName.setText(Session.getUserName());
        mTxtUserTel.setText(Session.getUserPhone());
        mTxtQyeName.setText(qyeName);
        mTxtQyeTel.setText("拨打：" + qyeTel);

        TextViewUtils.setSpannableStyleColor("50个字", 0, 2, mTxtWord, R.color.color_main);

        mEditRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mRemark = s + "";
                int l = 0;
                if (StringUtils.isEmpty(mRemark)) {
                    l = 50;
                    mLength = "";
                } else {
                    int m = mRemark.length();
                    if (m <= 50) {
                        l = 50 - m;
                        mLength = mRemark;
                    } else {
                        l = 0;
                        mEditRemark.setText(mLength);
                    }
                }
                TextViewUtils.setSpannableStyleColor(l + "个字", 0, (l + "").length(), mTxtWord, R.color.color_main);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.id_qye_subimt_btn, R.id.id_auth_tel_rela})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //提交
            case R.id.id_qye_subimt_btn:
                if (StringUtils.isEmpty(mRemark)) {
                    SVProgressHUD.showInfoWithStatus(aty, "请输入备注信息");
                } else {
                    ((FindOilPresenter) presenter).goToAuth(orgId, mRemark);
                }
                break;
            case R.id.id_auth_tel_rela:
                if (StringUtils.isEmpty(qyeTel)) {
                    return;
                }
                //拨打电话
                DialogUtils.showCommDialog(aty, "联系电话：" + qyeTel, "取消", "拨打", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.DialogDiss();
                        StringUtils.telPhone(aty, qyeTel);
                    }
                });
                DialogUtils.DialogShow();
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GO_TO_AUTH_SUCCESS)) {
                //企业认证成功
                SVProgressHUD.showSuccessWithStatus(aty, "提交成功，请耐心等待审核结果");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SVProgressHUDDismiss();
                        finish();
                    }
                }, 2000);
            }
        }
    }
}
